<template>
  <div class="app-container">
    <div class="default-box">
      <div class="filter-container">
        <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-plus" round @click="createDialog = true;isadd = true">
          新增
        </el-button>
        <el-button class="filter-item" type="success" icon="el-icon-s-unfold" round>
          展开
        </el-button>
        <el-button class="filter-item" type="success" icon="el-icon-s-fold" round>
          折叠
        </el-button>`
      </div>
      <!-- row-key="id" -->
      <el-table
        :key="tableKey"
        v-loading="listLoading"
        :data="list"
        fit
        highlight-current-row
        style="width: 100%;"
        :tree-props="{children: 'children'}"
      >
        <el-table-column label="权限名称" min-width="200">
          <template>
            <span>章鱼小丸子</span>
          </template>
        </el-table-column>
        <el-table-column label="图标" width="200">
          <template>
            <i class="el-icon-s-data" />
          </template>
        </el-table-column>
        <el-table-column label="权限类型" width="200">
          <template>
            <span>
              目录
            </span>
          </template>
        </el-table-column>
        <el-table-column label="状态" width="200">
          <template>
            <el-switch
              v-model="switchvalue"
              active-value="100"
              inactive-value="0"
            />
          </template>
        </el-table-column>
        <el-table-column label="排序" width="200">
          <template>
            <span>
              1
            </span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" class-name="small-padding fixed-width">
          <template>
            <i class="el-icon-edit" :style="{color:$store.state.settings.theme}" @click="createDialog = true;isadd = false" />
            <i class="el-icon-delete danger-color" @click="handleDelete" />
          </template>
        </el-table-column>
      </el-table>
      <!-- <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" /> -->
    </div>
    <!-- 展示新增、编辑对话框 -->
    <el-dialog
      :title="isadd?'新增菜单':'编辑菜单'"
      :visible.sync="createDialog"
      width="50%"
      custom-class="operate-dialog"
      top="30vh"
      center
    >
      <el-form :model="oprateform" label-width="80px">
        <el-form-item label="父级">
          <el-select v-model="oprateform.parent" class="fullwidth" placeholder="请选择">
            <el-option label="顶级" value="1" />
            <el-option label="后台权限" value="2" />
            <el-option label="数据统计" value="3" />
          </el-select>
        </el-form-item>
        <el-form-item label="权限名称">
          <el-input v-model="oprateform.name" placeholder="请输入权限名称" />
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="oprateform.address" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="图标" class="icon-form-item">
          <i :class="oprateform.icon" class="iconimg" />
          <el-popover placement="bottom-start" width="300" trigger="click">
            <el-row>
              <el-col v-for="(item,index) in iconsList" :key="index" :span="6">
                <el-button :icon="item" @click="selectIcon(item)" />
              </el-col>
            </el-row>
            <el-select slot="reference" popper-class="display-none" placeholder="" />
          </el-popover>
        </el-form-item>
        <el-form-item label="权限类型">
          <el-radio-group v-model="oprateform.permissiontype">
            <el-radio :label="3">目录</el-radio>
            <el-radio :label="6">菜单</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="排序">
          <el-input v-model="oprateform.sort" placeholder="请输入排序" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleReset">重置</el-button>
        <el-button type="primary" @click="handleOperate">提交</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { fetchList } from '@/api/article'
// import Pagination from '@/components/Pagination' // secondary package based on el-pagination
export default {
  name: 'Adminrole',
  // components: { Pagination },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        importance: undefined,
        title: undefined,
        type: undefined,
        sort: '+id'
      },
      switchvalue: true,
      oprateform: { // 新增、编辑条件
        name: '',
        address: '',
        icon: 'iconfont tf-icon-advertising-change',
        permissiontype: 3,
        sort: ''
      },
      isadd: true, // 是新增 否编辑
      createDialog: false, // 是否展示新增、编辑对话框
      iconsList: ['iconfont tf-icon-loading', 'iconfont tf-icon-dynamic-hide', 'iconfont tf-icon-order-receiving-time']
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        // console.log(response.data.items)
        var children = [{
          id: 31,
          date: '2016-05-01',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1519 弄'
        },
        {
          id: 32,
          date: '2016-05-01',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1519 弄'
        }]
        response.data.items.forEach(item => {
          item.children = children
        })
        this.list = response.data.items
        this.total = response.data.total
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
    },
    selectIcon(icon) {
      this.oprateform.icon = icon
    },
    // 新增、编辑
    handleOperate() {
      this.createDialog = false
    },
    // 重置
    handleReset() {
      this.oprateform = {
        name: '',
        address: '',
        icon: '',
        permissiontype: 3,
        sort: ''
      }
    },
    // 删除
    handleDelete(row, index) {
      this.$confirm('确定要删除该菜单？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        center: true,
        customClass: 'text-center'
      }).then(() => {
        this.$notify({
          title: '成功',
          message: '删除成功',
          type: 'success',
          duration: 2000
        })
        // this.$message({
        //   type: 'success',
        //   message: '删除成功!'
        // })
      }).catch(() => {

      })
      this.list.splice(index, 1)
    }
  }
}
</script>
<style scoped lang="scss">
  [class^=el-icon-], [class*=" el-icon-"]{
    font-size: 16px;
  }
  .small-padding i{
    margin-right: 12px;
  }
  .small-padding i:last-child{
    margin-right: 0px;
  }
  ::v-deep{
    .icon-form-item{
      .el-form-item__content{
        position: relative;
        i.iconimg{
          position: absolute;
          left: 16px;
          z-index: 9;
        }
        .el-input--medium .el-input__inner{
            width: 80px;
          }
          .el-input__suffix{
            border-left: 1px solid #DCDFE6;
            padding-left: 5px;
          }
      }
    }
  }
</style>
